exports.install = function (Vue, options) {
  Vue.prototype.$ajax = function (option) {
    option = option || {}
    option.url = option.url || ''
    option.async = option.async || true
    option.data = option.data || null
    option.formData = option.formData || false
    option.success = option.success || function () {}
    option.error = option.error || function () {}
    option.busy = option.busy || function () {}
    option.type = option.type || 'POST'
    let xhr = new XMLHttpRequest()
    xhr.open(option.type, option.url, option.async)
    // 处理formData
    if (option.formData) {
      xhr.send(option.data)
    } else {
        if(option.type=='POST'){
            let params = []
            for (let key in option.data) {
                let data = encodeURIComponent(option.data[key])
                params.push(key + '=' + data)
            }
            let postData = params.join('&')
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8')
            xhr.send(postData)
        }else{
            xhr.send();
        }
    }
    xhr.onreadystatechange = () => {
      if (xhr.readyState === 4 && xhr.status === 200) {
        let data = JSON.parse(xhr.responseText)
        if (data.resultCode === 0) {
          option.success(data)
        } else if (data.resultCode === 1005) {
            sessionStorage.removeItem('userId')
          // this.$login()
        }else if(data.resultCode === 2001){
            option.success(data)
        }
        // else if(data.resultCode === 2001){
        //
        // }
        else{
            alert(data.resultMessage)
            option.error(data)
        }
      } else if (xhr.status === 500) {
        console.log('尝试与服务器连接失败，请稍后重试。')
        option.busy()
      }
    }
  }
  Vue.prototype.$login = function (cb){
    if (sessionStorage.getItem("userId")) {
        if(cb) cb();
      return true
    } else {
      window.location ='https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4553fda6b826067b&redirect_uri=https://quxiangba.cn/h5/index.html&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect';
      // let ua = window.navigator.userAgent.toLowerCase()
      // if (ua.match(/MicroMessenger/i) == 'micromessenger') {
      //   window.location = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx91974bcd7122c1a8&redirect_uri=http%3a%2f%2fwww.malikong.cn%2fweb%2findex.html%23%2fweixin&response_type=code&scope=snsapi_base&state=1#wechat_redirect'
      // } else {
      //   this.$router.push('/login')
      // }
      return false
    }
  }
}
